package com.kuanter.kuanterauto.baseui;



import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Toast;

import com.kuanter.kuanterauto.R;

/**
 * 基础ActivityGroup 主界面用到
 * @author soup
 *
 */
@SuppressWarnings("deprecation")
public abstract class BaseActivityGroup extends ActivityGroup implements OnClickListener{
	
	ProgressDialog progressDialog=null;
	
	
	@Override
	public abstract void onClick(View v);
	
	/** 打开新的Activity */
	protected void qStartActivity(Class<?> cls, Bundle bundle) {
		Intent intent = new Intent();
		if (bundle != null)
			intent.putExtras(bundle);
		intent.setClass(this, cls);
		startActivity(intent);
	}
	
	/** 打开新的Activity */
	protected void qStartActivity(Class<?> cls, Bundle bundle,String action) {
		Intent intent = new Intent(action);
		if (bundle != null)
			intent.putExtras(bundle);
		intent.setClass(this, cls);
		startActivity(intent);
	}

	/** 打开新的Activity for result */
	protected void qStartActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
		Intent intent = new Intent();
		if (bundle != null)
			intent.putExtras(bundle);
		intent.setClass(this, cls);
		startActivityForResult(intent, requestCode);
	}

	/** 带结果返回上一个activity， 配合qStartActivityForResult使用 */
	protected void qBackForResult(int resultCode, Bundle bundle) {
		Intent intent = new Intent();
		if (bundle != null)
			intent.putExtras(bundle);
		setResult(resultCode, intent);
		finish();
	}

	/** 回到之前的Activity */
	protected void qBackToActivity(Class<?> cls, Bundle bundle) {
		Intent intent = new Intent();
		if (bundle != null)
			intent.putExtras(bundle);
		intent.setClass(this, cls);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);
	}
	
	/** 回到之前的Activity */
	protected void qBackToActivity(Class<?> cls, Bundle bundle,String action) {
		Intent intent = new Intent(action);
		if (bundle != null)
			intent.putExtras(bundle);
		intent.setClass(this, cls);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);
	}
	
	
	protected void showToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}

	// 带位置的Toast
	protected void showToast(String message, int left, int top) {
		Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.TOP | Gravity.LEFT, left, top);
		toast.show();
	}
	
	/**
	 * 显示加载
	 */
	protected void showProgress(String message) {
		progressDialog = new ProgressDialog(BaseActivityGroup.this);
		progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		progressDialog.setMessage(message);
		progressDialog.show();
	}
	
	/**
	 * 完成加载
	 */
	protected void finishProgress() {
		if (progressDialog != null) {
			progressDialog.dismiss();
		}
	}
	
	/**---activity 动画**/
	protected void startActivity_anim(int num){
		switch (num) {
		case 0://淡入淡出效果
			overridePendingTransition(R.anim.fade, R.anim.hold);
			break;
		case 1://放大淡出效果
			overridePendingTransition(R.anim.my_scale_action,
					R.anim.my_alpha_action);
			break;
		case 2://转动淡出效果1
			overridePendingTransition(R.anim.scale_rotate,
					R.anim.my_alpha_action);
			break;
		case 3://转动淡出效果2
			overridePendingTransition(R.anim.scale_translate_rotate,
					R.anim.my_alpha_action);
			break;
		case 4://左上角展开淡出效果
			overridePendingTransition(R.anim.scale_translate,
					R.anim.my_alpha_action);
			break;
		case 5://压缩变小淡出效果
			overridePendingTransition(R.anim.hyperspace_in,
					R.anim.hyperspace_out);
			break;
		case 6://右往左推出效果
			overridePendingTransition(R.anim.push_left_in,
					R.anim.push_left_out);
			break;
		case 7://下往上推出效果
			overridePendingTransition(R.anim.push_up_in,
					R.anim.push_up_out);
//			overridePendingTransition(R.anim.slide_up_in,
//					R.anim.slide_down_out);
			break;
		case 8://左右交叉效果
			overridePendingTransition(R.anim.slide_left,
					R.anim.slide_right);
			break;
		case 9://放大淡出效果
			overridePendingTransition(R.anim.wave_scale,
					R.anim.my_alpha_action);
			break;
		case 10://缩小效果
			overridePendingTransition(R.anim.zoom_enter,
					R.anim.zoom_exit);
			break;
		case 11://上下交错效果
			overridePendingTransition(R.anim.slide_up_in,
					R.anim.slide_down_out);
			break;
		}
	}
	
	// 判断网络是否可用
		public boolean CheckNetworkState() {
			boolean flag = false;
			ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			if (manager.getActiveNetworkInfo() != null) {
				flag = manager.getActiveNetworkInfo().isAvailable();
			}
			if (!flag) {
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setIcon(android.R.drawable.ic_dialog_alert);
				builder.setTitle("网络状况");
				builder.setMessage("当前网络不可用，是否设置网络");
				builder.setPositiveButton("设置",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
//								Intent mIntent = new Intent("/");
//								ComponentName comp = new ComponentName(
//										"com.android.settings",
//										"com.android.settings.WirelessSettings");
//								mIntent.setComponent(comp);
//								mIntent.setAction("android.intent.action.VIEW");
//								startActivity(mIntent);
								
								if (android.os.Build.VERSION.SDK_INT > 10) {
									// 3.0以上打开设置界面
									startActivity(new Intent(
											android.provider.Settings.ACTION_SETTINGS));
								} else {
									startActivity(new Intent(
											android.provider.Settings.ACTION_WIRELESS_SETTINGS));
								}
							}
						});
				builder.setNegativeButton("取消",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.cancel();
							}
						});
				builder.create();
				builder.show();
			}

			return flag;

		}

}
